﻿define(['admin/app', "service/admin/msgService"], function (app) {
    "use strict";

    app.controller('msgController', function ($scope, $state, $stateParams, $location, utils, w5cValidator, msgService) {
        $scope.psdisabled = false;
        $scope.arr_send_type = [{ id: 1, name: "APP消息", }, { id: 2, name: "短信", }, { id: 3, name: "微信" }];

        var methods = {
            search: function () {
                msgService.gets({ name: $scope.name }).success(function (response) {
                    $scope.list = response.data;
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    msgService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                }
                else {
                    $scope.model = { id: 0, send_type: [] };
                    $scope.breadcrumb = '新增';
                }
                w5cValidator.setRules({
                    name: { required: '请填写名称' }
                });
            },
            get_type_name: function (sendtype) {
                var names = [];
                if (sendtype.indexOf(1) >= 0) names.push('APP消息');
                if (sendtype.indexOf(2) >= 0) names.push('短信');
                if (sendtype.indexOf(3) >= 0) names.push('微信');
                return names.join(',');
            },
            goBack: function () {
                history.back();
            },
            save: function () {
                if ($stateParams.id > 0) {
                    msgService.put($scope.model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
                else {
                    msgService.post($scope.model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            remove: function (model) {
                var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    msgService.delete(model.id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            utils.remove($scope.model, model);
                    })
                })
            },
            sel_send_type: function (type) {
                var index = $scope.model.send_type.indexOf(type);
                if (index === -1) $scope.model.send_type.push(type);
                else $scope.model.send_type.splice(index, 1);
            }
        }

        angular.extend($scope, methods);
        if ($stateParams.action != undefined)
            methods.edit();
        else
            methods.search();
    });
});
